<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Huifang extends Model
{

    use SoftDelete;


    // 表名
    protected $name = 'huifang';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'sex_text',
        'intime_text',
        'outtime_text',
        'yytime_text',
        'hftime_text'
    ];


    public function getSexList()
    {
        return ['male' => __('Sex male'), 'female' => __('Sex female')];
    }


    public function getSexTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sex']) ? $data['sex'] : '');
        $list  = $this->getSexList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIntimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['intime']) ? $data['intime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOuttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['outtime']) ? $data['outtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getYytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['yytime']) ? $data['yytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getHftimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['hftime']) ? $data['hftime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setIntimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setOuttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setYytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setHftimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }



}
